package org.i1510.wuyo.controller;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.i1510.common.utils.ConfPropertiesUtils;
import org.i1510.common.utils.StringUtils;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.i1510.common.utils.web.WebUtils;
import org.i1510.wuyo.bean.BaseCfgBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;






@Controller
@RequestMapping(value = "/admin")
public class BaseCfgController {
	private static final Logger logger = LoggerFactory.getLogger(BaseCfgController.class);
	
	private ResultMapper mapper;
	
	/**
	*@Author Rambo
	*@Desc：保存对象
	*@param baseCfg
	*@param errors
	*@return TODO
	*@Version  BaseCfgController.java,v 1.1 2013-11-15 下午4:20:56 
	*/
	@RequestMapping(value = "/baseCfg/save", method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request,@Valid @ModelAttribute("baseCfg") BaseCfgBean baseCfg,BindingResult errors) {
		
		if(errors.hasErrors()){			
			mapper = WebUtils.getMapper4Errors(errors);
		}else{
			ConfPropertiesUtils pUtils = new ConfPropertiesUtils();
			Map<String,String> map = new HashMap<String,String>();
			if(!StringUtils.isStringEmpty(baseCfg.getDescs())){
				map.put("web.desc", baseCfg.getDescs());
			}
			if(!StringUtils.isStringEmpty(baseCfg.getEmailTemp())){
				map.put("email.beta.audit.success", baseCfg.getEmailTemp());
			}
			if(!StringUtils.isStringEmpty(baseCfg.getFindEmailTemp())){
				map.put("email.find.pw", baseCfg.getFindEmailTemp());
			}
			if(!StringUtils.isStringEmpty(baseCfg.getIcp())){
				map.put("web.icp", baseCfg.getIcp());
			}
			if(!StringUtils.isStringEmpty(baseCfg.getInfoScore())){
				map.put("web.infoScore", baseCfg.getInfoScore());
			}
			if(!StringUtils.isStringEmpty(baseCfg.getLoginForScore())){
				map.put("web.loginForScore", baseCfg.getLoginForScore());
			}
			if(!StringUtils.isStringEmpty(baseCfg.getKey())){
				map.put("web.key", baseCfg.getKey());
			}
			
			pUtils.updatePro("/config/common/conf.properties",map, UserUtils.getSessionUserBean(request).getSysUser().getName());
			
		}
		mapper = mapper.generateSuccess();
		return mapper.toJson();
	}
	@RequestMapping(value = "/baseCfg/view", method = RequestMethod.GET)
	@ResponseBody
	public String view(HttpServletRequest request) {
		
			ConfPropertiesUtils pUtils = new ConfPropertiesUtils();
			Properties pro = pUtils.loadPro("/config/common/conf.properties");
			mapper = mapper.generateSuccess();
			
			BaseCfgBean baseCfg = new BaseCfgBean();
			baseCfg.setDescs(pro.getProperty("web.desc"));
			baseCfg.setEmailTemp(pro.getProperty("email.beta.audit.success"));
			baseCfg.setFindEmailTemp(pro.getProperty("email.find.pw"));
			baseCfg.setIcp(pro.getProperty("web.icp"));
			baseCfg.setInfoScore(pro.getProperty("web.infoScore"));
			baseCfg.setLoginForScore(pro.getProperty("web.loginForScore"));
			baseCfg.setKey(pro.getProperty("web.key"));
			
			mapper.appendResult("data", baseCfg);
			
		
		
		return mapper.toJson();
	}
	
}